package com.shhm.business.service.impl.business;

import com.shhm.business.dto.request.SubmitOrderReq;
import com.shhm.business.dto.response.OrderDetailResp;
import com.shhm.business.dto.response.ProductInfoResp;
import com.shhm.business.mapper.*;
import com.shhm.business.service.business.BusinessProductInfoService;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.security.SecurityUser;
import com.shhm.common.entity.common.EvaluationEntity;
import com.shhm.common.entity.common.OrderEntity;
import com.shhm.common.utils.ResponseUtil;
import com.shhm.common.utils.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/9/12 下午3:50
 */
@Service
@RequiredArgsConstructor
public class BusinessProductInfoServiceImpl implements BusinessProductInfoService {
    private final ProductInfoMapper productInfoMapper;
    private final ProductMapper productMapper;
    private final ProductStatusMapper productStatusMapper;
    private final EvaluationMapper evaluationMapper;
    private final OrderMapper orderMapper;

    /**
     * 通过id查询商品详情
     * @param productId
     * @return
     */
    @Override
    public AjaxResultPlus getProductInfo(Integer productId) {
        ProductInfoResp productInfoResp = productInfoMapper.selectProductInfo(productId);
        productInfoResp.handleProductDetailImages();
        return AjaxResultPlus.success(productInfoResp,false);
    }

    /**
     * 查询卖家其他上架的商品
     * @param sellerId
     * @return
     */
    @Override
    public AjaxResultPlus sellerProductList(Integer sellerId) {
        return AjaxResultPlus.success(productMapper.selectSellerShelfProducts(sellerId),false);
    }

    @Transactional
    @Override
    public AjaxResultPlus createOrderByProductId(Integer productId) {
        SecurityUser customerObj = (SecurityUser) SecurityContextHolder
                .getContext()
                .getAuthentication()
                .getPrincipal();
        long customerId = customerObj.getUserId();
        OrderEntity oe = new OrderEntity();
        String randomNumber = StringUtil.createRandomNumber("ORD" + customerId);
        oe.setOrderNum(randomNumber);
        oe.setProductId(productId);
        oe.setCustomId(customerId);
        oe.createLocalDateTime();

        Integer orderByProductId = productMapper.createOrderByProductId(oe);
        Integer update=productStatusMapper.customUpdate(productId);
        return AjaxResultPlus.custom(200,true).put("msg","下单成功,等待进入支付操作").put("data",oe.getOrderNum());
    }

    @Override
    public AjaxResultPlus submitOrder(SubmitOrderReq req) {
        req.operateType();
        if (productInfoMapper.submitOrder(req)<=0) return AjaxResultPlus.warn(req.getFailTips());
        return AjaxResultPlus.custom(200,true)
                .put("msg",req.getSuccessTips())
                .put("data",req.getOrderNum());
    }

    @Override
    public AjaxResultPlus getOrderInfo(String orderNum) {
        SecurityUser userInfo = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        OrderDetailResp orderDetailResp = productInfoMapper.selectOrderInfo(orderNum, userInfo.getUserId());
        if (Objects.isNull(orderDetailResp)) return AjaxResultPlus.warn("该订单不存在");
        return AjaxResultPlus.success(orderDetailResp,false);
    }

    @Transactional
    @Override
    public AjaxResultPlus createProductComment(EvaluationEntity entity) {
        SecurityUser userInfo = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        long userId = userInfo.getUserId();
        entity.setUserId(userId);
        entity.createLocalDateTime();
        AjaxResultPlus res=null;
        res = ResponseUtil.sqlExeIsSuccess(evaluationMapper.insert(entity));
        res = ResponseUtil.sqlExeIsSuccess(orderMapper.evaluationOrder(entity.getProductNum()));
        return res;
    }

    @Transactional
    @Override
    public AjaxResultPlus cancelOrder(SubmitOrderReq req) {
        req.operateType();
        if (productInfoMapper.submitOrder(req)<=0) return AjaxResultPlus.warn(req.getFailTips());
        productInfoMapper.productStatxusReturn(req.getOrderNum());
        return AjaxResultPlus.custom(200,true)
                .put("msg",req.getSuccessTips())
                .put("data",req.getOrderNum());
    }

//    @Override
//    public AjaxResultPlus updateProductComment(EvaluationEntity entity) {
//        return ResponseUtil.sqlExeIsSuccess(evaluationMapper.updateById( entity));
//    }
}
